var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var open=wx.getStorageSync('open'); 
        var deviceInfo = wx.getDeviceInfo();
        console.log(deviceInfo)
        let d = app.globalData;
        this.getIndex();
        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
          })
        this.setData({
            //id:id,
            open:open,
            width:d.windowWidth,
            height:d.windowHeight,
            statusBarHeight:d.statusBarHeight,
            navHeight:d.navHeight,
            marginTop:d.navTitleTop,
            bottom:d.bottom,
            domain:d.domain,
            imgUrl:d.imgUrl,
            platform:deviceInfo.platform
        })
    },
    goto(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        console.log(url);
        var index=e.currentTarget.dataset.index;
        app.goto(url,index)
      // console.log(url);
    },
    sign:function(){
        let that=this;
        var open=wx.getStorageSync('open'); 
        wx.request({
            url: app.globalData.domain+'/wechat/home/sign',
            data:{from:1,user_id:open.user_id},
            success:res=>{
                console.log(res);
                var data=res.data;
                if(data.status==200){
                    wx.showModal({
                        title: '系统提示',
                        content:data.message,
                        success:function(res){
                           // that.useClose();
                          //  that.getList();
                            that.setData({
                                is_sign:1
                            })
                        }
                    })
                }else{
                    wx.showModal({
                        title: '系统提示',
                        content:data.message,
                        success:function(res){
                            that.setData({
                                is_sign:1
                            })
                            return false;
                        }
                    })
                }
            }
        })
    },
    getIndex:function(){
        let that = this;    
        console.log('coupon----getIndex')  
        var store_id = wx.getStorageSync('store_id');
        var open = wx.getStorageSync('open');
        wx.request({
          url: app.globalData.domain+'/wechat/mine/Index',
          data:{from:1,open_id:open.id,user_id:open.user_id},
          success:res=>{
              console.log(res)
              var d=res.data;
              var open = d.open;
              var phone;
           if(open.phone){
                phone = that.strPlace(open.phone)
           }
           open.phone = phone;
            that.setData({
            servicePhone:d.servicePhone,
            user:d.user,
            open:open
         

            })
          }
      })

    },

    call(e){
        console.log(e);
        var callPhone = e.currentTarget.dataset.phone;
        wx.makePhoneCall({//调用拨打电话的函数
            phoneNumber: callPhone,
          })
  
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that = this;
        var open=wx.getStorageSync('open'); 
        if(open.phone){
            let regx = /(1[3|4|5|6|7|8|9][\d]{9}|0[\d]{2,3}-[\d]{7,8}|400[-]?[\d]{3}[-]?[\d]{4})/g;
            var str = open.phone;
            console.log("替换前的文本内容：",str)
            // 查找出文本中的所有手机号码
            var phoneNums = str.match(regx);
            console.log("phoneNums",phoneNums)
            if(phoneNums){
                for(var i= 0;i<phoneNums.length;i++){
                    //手机号全部替换
                    var temp  = phoneNums[i]
                    //隐藏手机号中间4位(例如:153012345678,隐藏后为153****4567)
                    temp = temp.replace(/^(\d{3})\d{4}(\d{4})$/,"$1****$2");
                    str = str.replace(phoneNums[i],temp);
                }
            }
            console.log('phone'+str)
        
         open.phone = str;
         console.log(open)
        }
        this.setData({
            //id:id,
            open:open
        })
        var  tab = wx.getStorageSync('tabBar');
     if(tab == '' || tab == undefined){
         app.getMenu();
         setTimeout(function(){ 
         //console.log("延时1秒后执行");
           that.getTabBar().init('storeWait');
       }, 1000);
     }else{
         this.getTabBar().init('store');
     }
         
  
       
       
        // this.getCity(); 
         //this.getLocation();
       },
      strPlace(str){
        let regx = /(1[3|4|5|6|7|8|9][\d]{9}|0[\d]{2,3}-[\d]{7,8}|400[-]?[\d]{3}[-]?[\d]{4})/g;
        //var str = open.phone;
        console.log("替换前的文本内容：",str)
        // 查找出文本中的所有手机号码
        var phoneNums = str.match(regx);
        console.log("phoneNums",phoneNums)
        if(phoneNums){
            for(var i= 0;i<phoneNums.length;i++){
                //手机号全部替换
                var temp  = phoneNums[i]
                //隐藏手机号中间4位(例如:153012345678,隐藏后为153****4567)
                temp = temp.replace(/^(\d{3})\d{4}(\d{4})$/,"$1****$2");
                str = str.replace(phoneNums[i],temp);
            }
        }
        console.log('phone'+str);
        return str;
      },
       getPhoneNumber (e){
        let that=this;
        var open=wx.getStorageSync('open');
         
        if(open == ''){
            wx.navigateTo({
                url: '../login/index',
              })
        }
        if(open == undefined){
            wx.navigateTo({
              url: '../login/index',
            })
        }
         console.log(e);   
                wx.request({
                    url:app.globalData.domain+'/wechat/login/getPhone',
                    data:{
                      code:e.detail.code,
                       id:open.id,
                       type:3
                    },
                    method:'GET',
                    header:{
                        'content-type':'application/json'
                    },
                    success:function(res){
                      console.log(res)
                        //wx.setStorageSync('PhoneNumber',res.data.phone);
                        
                        if(res.statusCode==200){
                            var data = res.data;
                          open.phone = data.phone;
                          wx.setStorageSync('open', data.open);
                          wx.setStorageSync('user', data.user);
                          that.setData({
                              open:open,
                              login:false,
                              phone:res.data.phone
                          })
                          app.showModals('获取手机号成功');
                        }else{
                            app.showModals('获取手机号失败');
                        } 
                        //that.show();
                        //console.log("手机号为");
                        //console.log(res);
                    },
                    fail:function(err){
                        console.log(err);
                    }
                })
       
      },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})